<?php
class CollectBase {
	
	public static function curl($url)
	{
            $pageinfo = array();   
            $pageinfo[content_type] = '';   
            $pageinfo[charset] = '';   
            $pageinfo[title] = '';   
            $pageinfo[description] = '';   
            $pageinfo[keywords] = '';   
            $pageinfo[body] = '';   
            $pageinfo['httpcode'] = 200;   
            $pageinfo['all'] = '';   
			$pageinfo['html'] = '';               
              
            $ch = curl_init();   
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");   
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);   
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);   
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);   
            curl_setopt($ch, CURLOPT_TIMEOUT, 8);   
            curl_setopt($ch, CURLOPT_FILETIME, 1);   
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);   
            //curl_setopt($ch, CURLOPT_HEADER, 1);         
            curl_setopt($ch, CURLOPT_URL,$url);   
               
            $curl_start = microtime(true);   
            $pageinfo['html'] = curl_exec ($ch);    
            $curl_time = microtime(true) - $curl_start;   
            if( curl_error($ch) ) {   
                $pageinfo['httpcode'] = 505;  //gate way error   
                //echo 'Curl error: ' . curl_error($ch) ."\n";   
                return $pageinfo;   
            }   
               
            //print_r(curl_getinfo($ch));   
            $pageinfo['httpcode'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);   
            //echo curl_getinfo($ch,CURLINFO_CONTENT_TYPE)."\n";   
            $pageinfo[content_type] = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);   
            if(intval($pageinfo['httpcode']) <> 200 or !preg_match('@text/html@',curl_getinfo($ch,CURLINFO_CONTENT_TYPE) )   ) {   
                    //print_r(curl_getinfo($ch) );   
                    //exit;   
                    return $pageinfo;   
            }   
            preg_match('/charset=([^\s\n\r]+)/i',curl_getinfo($ch,CURLINFO_CONTENT_TYPE),$matches); //从header 里取charset   
            if( trim($matches[1]) ) {   
                $pageinfo[charset] = trim($matches[1]);   
            }   
            //echo $pageinfo[charset];   
            //exit;   
            curl_close ($ch);   
            //echo $store;  
			return $pageinfo;		
	}
    /**  
     * 去掉所有的HTML标记和JavaScript标记  
     */  
    public static function replaceHtmlAndJs($document)   
    {   
             $document = trim($document);   
             if (strlen($document) <= 0)   
             {   
              return $document;   
             }   
             $search = array (         
                                                "'<script[^>]*?>.*?</script>'si",  // 去掉 javascript   
                              "'<[\/\!]*?[^<>]*?>'si",          // 去掉 HTML 标记   
                              "'[\r\n\s+]'",                // 去掉空白字符   
                              "'&(\w+);'i"              // 替换 HTML 实体   
                             );                    // 作为 PHP 代码运行   
               
             $replace = array ( "", "", "", ""  );   
               
             return @preg_replace ($search, $replace, $document);   
      
    } 
	
	public static function replaceHtmlAndJs2($document)   
    {   
			 $document = trim($document);  
             $document=preg_replace("/<script[^<>]*>.*<\/script>/Uisx","",strip_tags($document,"<img><p><br><span><style>"));
			 $document=preg_replace("/<style[^<>]*>.*<\/style>/Uisx","",$document);
			 $document=preg_replace("/&(\w+);/Uisx","",$document);
			 $document=preg_replace("/(\n\s*\r)+/m","",$document);
			 $document=preg_replace("/$/m","",$document);
			 preg_match_all("/<img([^<>]*)>/Uisx",$document,$nn);
			 for($i=0;$i<count($nn[1]);$i++){
				 preg_match_all("/file=\"([^<>]*)\"/Uisx",$nn[0][$i],$nn3);$src=$nn3[1][0];
				 if($src==""){
				 preg_match_all("/src=\"([^<>]*)\"/Uisx",$nn[0][$i],$nn2);$src=$nn2[1][0];
				 }				 
				 $document=str_replace($nn[0][$i],"[img]".$src."[/img]",$document);
			 }
			 $document=preg_replace("/本帖.*[0-9\-\s\:]+编辑/Uisx","",$document);
			 $document=preg_replace("/(<br\s*\/>\s*)+/isx","\r\n",$document);
			 $document=preg_replace("/\[\/img\]([^<>]*)KB\)/Uisx","[/img]",$document);
			 $document=preg_replace("/本帖.*[0-9\-\s\:]+编辑/Uisx","",$document);
			 $document=preg_replace("/<span[^<>]*style=\\\?\"display\:none\\\?\"[^<>]*>.*<\/span>/Uisx","",$document);
			 $document=preg_replace("/<span[^<>]*>|<\/\s*span>/Uisx","",$document);
			 $document=preg_replace("/\(微博\)/m","",trim($document));
			 $document=str_replace('\r\n',"",$document);
			 $document=preg_replace("/(\\\r|\\\n)+/imx","\r\n",$document);    
			 $document=preg_replace("/<(\/?)\s*(br|p)[^<>]*>+/isx","\r\n",$document);    
			 $document=preg_replace("/\&ldquo\;/Uisx","\"",$document);    
			 $document=preg_replace("/\&rdquo\;/Uisx","\"",$document);    
			 $document=preg_replace("/(\n\s*\r)+/m","\r\n",$document);
			 $document=preg_replace("/<(\/?)(strong|b)\b([^<>]*)>/Uisx","",$document); 
             return $document;   
      
    } 
	
	public static function trims($str){
	   $str=preg_replace("/\&nbsp\;/Uisx","",$str);
	   $str=trim($str);
	   return $str;	
	}
}
?>